/* ----------------------------------------------------------------------------------------------------------------
 * Status
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.status 
{
	import flash.geom.Point;
	import pure.engine.system.ITickObject;
	import pure.engine.system.ProcessManager;
	import pure.engine.entity.IEntity;
	import pure.utils.DesN;
	
	/**
	 * @inheritDoc
	 * 
	 * 状态
	 * 
	 * @author ＃Pure
	 */
public class Status implements IStatus, ITickObject
{
	
	
	protected var _entity:IEntity;
	protected var _reactionList:Object;
	protected var _ticked:Boolean, _framed:Boolean;
	
	
	public static var cachedPoint:Point = new Point();
	
	
	public function get entity():IEntity{return _entity;}
	public function set entity(E:IEntity):void
	{
		if(_entity)
		{
			throw new Error('Status -＃set entity - 状态只能赋值一次.');
		}
		_entity = E;
	}
	
	public function setTicked(flag:Boolean, priority:Number = 0.0):void
	{
		if (_ticked != flag)
		{
			_ticked = flag;
			if(_ticked)
				ProcessManager.instance.addTickObject(this, priority);
			else
				ProcessManager.instance.removeTickedObject(this);
		}
	}
	

	
	public function locate(movement:Number, finalRotation:Number):Point
	{
		var angle:Number;
		
		angle = (finalRotation - 90) * DesN.PI_OVER_ONE_EIGHTY;
		cachedPoint.x = _entity.x + movement * Math.cos(angle);
		cachedPoint.y = _entity.y + movement * Math.sin(angle);
		return cachedPoint;
	}
	
	
	/**
	 * @inheritDoc
	 */
	public function absorb():void
	{
	}

	
	public function registerReaction(reactionName:String, reactionCallback:Function):void
	{
		if (!_reactionList)
		{
			_reactionList = { };
		}
		
		if (_reactionList[reactionName] != null)
		{
			throw new Error('Entity - ＃registerReaction - Reaction id "'+reactionName+'" has existed.');
		}
		
		_reactionList[reactionName] = reactionCallback;
	}
	
	
	
	public function doReaction(reactionName:String):void
	{
		if (_reactionList)
		{
			var reactionCallback:Function = _reactionList[reactionName];
			if (reactionCallback != null)
			{
				reactionCallback();
			}
		}
	}

	

	public function onTick(elapsedT:Number):void
	{
	}
		
	
	public function exit():void
	{
		setTicked(false);
		_entity = null;
		_reactionList = null;
		
		// 继承下面方法
		_exit();
	}
	
	
	
	protected function _exit():void
	{
	}
}

}